﻿using System.Globalization;
using NHibernate.SqlTypes;
using NHibernate.UserTypes;
using System;
using System.Data;


namespace epicenter2010.NH.Customisation.UserTypes
{
	/// <summary>
	/// bstack @ 20/10/2010
	/// CultureInfo user type
	/// </summary>
	public class CultureInfoType : IUserType
	{
		/// <summary>
		/// Sql types
		/// </summary>
		public SqlType[] SqlTypes
		{
			get
			{
				return new SqlType[1] { new SqlType(DbType.Int32) };
			}
		}

		/// <summary>
		/// Returned type
		/// </summary>
		public Type ReturnedType
		{
			get { return typeof(CultureInfo); }
		}

		/// <summary>
		/// Is mutable
		/// </summary>
		public bool IsMutable
		{
			get { return false; }
		}


		/// <summary>
		/// Equals
		/// </summary>
		/// <param name="object1"></param>
		/// <param name="object2"></param>
		/// <returns></returns>
		public new bool Equals(
			object object1, 
			object object2)
		{
			if (object1 == null)
			{
				return false;
			}

			return object1.Equals(object2);
		}


		/// <summary>
		/// Get hash code
		/// </summary>
		/// <param name="subject"></param>
		/// <returns></returns>
		public int GetHashCode(
			object subject)
		{
			return subject.GetHashCode();
		}


		/// <summary>
		/// Null safe get
		/// </summary>
		/// <param name="dataReader"></param>
		/// <param name="names"></param>
		/// <param name="owner"></param>
		/// <returns></returns>
		public object NullSafeGet(
			IDataReader dataReader, 
			string[] names, 
			object owner)
		{
			int _fieldIndex;


			_fieldIndex = dataReader.GetOrdinal(names[0]);

			return CultureInfo.GetCultureInfo(Convert.ToInt32(dataReader[_fieldIndex]));
		}


		/// <summary>
		/// Null safe set
		/// </summary>
		/// <param name="dbCommand"></param>
		/// <param name="value"></param>
		/// <param name="index"></param>
		public void NullSafeSet(
			IDbCommand dbCommand, 
			object value, 
			int index)
		{
			IDataParameter _dataParameter;


			_dataParameter = (IDataParameter)dbCommand.Parameters[index];
			_dataParameter.Value = ((CultureInfo)value).LCID;
		}


		/// <summary>
		/// Deep copy
		/// </summary>
		/// <param name="subject"></param>
		/// <returns></returns>
		public object DeepCopy(
			object subject)
		{
			if (subject == null)
			{
				return null;
			}

			return (CultureInfo)subject;
		}


		/// <summary>
		/// Replace
		/// </summary>
		/// <param name="original"></param>
		/// <param name="target"></param>
		/// <param name="owner"></param>
		/// <returns></returns>
		public object Replace(
			object original, 
			object target, 
			object owner)
		{
			return original;
		}


		/// <summary>
		/// Replace
		/// </summary>
		/// <param name="cached"></param>
		/// <param name="owner"></param>
		/// <returns></returns>
		public object Assemble(
			object cached, 
			object owner)
		{
			return cached;
		}


		/// <summary>
		/// Disassemble
		/// </summary>
		/// <param name="subject"></param>
		/// <returns></returns>
		public object Disassemble(
			object subject)
		{
			return subject;
		}
	}
}
